package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SysUser;
import com.itheima.service.SysUserService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


/**
 * Author  赵晴
 * Date 2020/07/08  15:17
 * Version 1.0
 */

@RequestMapping("/sysUserController")
@RestController
@Slf4j
public class SysUserController {
    @Reference
    SysUserService sysUserService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug ( "用户分页查询请求参数："+queryPageBean );
        PageResult pageResult=sysUserService.findPage(queryPageBean);
        log.debug ( "查询结果："+ pageResult );
        return pageResult;
    }

    //添加系统用户信息
    @RequestMapping("/add")
    public Result add(@RequestBody SysUser sysUser,Integer[] roleIds){
        //默认的加密工具类
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder ();
        // 加密调用的方法是 encode
        String encode = passwordEncoder.encode(sysUser.getPassword ());
        sysUser.setPassword ( encode );
        log.debug ( "新增系统用户获取参数："+sysUser );
        log.debug ( "选择的角色id"+ Arrays.toString ( roleIds )  );
        sysUserService.add(sysUser,roleIds);
        log.debug ( MessageConst.ADD_SYSUSER_SUCCESS );
        return new Result ( true,MessageConst.ADD_SYSUSER_SUCCESS  );
    }

    //编辑用户的回显信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug ( "sysUserId:"+id );
        SysUser sysUser=sysUserService.findById(id);
        log.debug ( "sysUser"+sysUser );
        return new Result ( true,MessageConst.QUERY_SYSUSER_SUCCESS,sysUser );
    }
    //根据用户id，查询管理的角色
    @RequestMapping("/findRoleIdsBySyuUserId")
    public Result findRoleIdsBySyuUserId(Integer sysUserId){
        log.debug ( "根据用户id查询角色id,获取到的参数："+sysUserId );
        List<Integer> list=sysUserService.findRoleIdsBySyuUserId(sysUserId);
        log.debug ( "用户关联的角色id"+ list );
        return new Result ( true,MessageConst.QUERY_ALL_ROLES_SUCCESSFULLY,list );
    }
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser,Integer[] roleIds){
        log.debug ( "编辑的用户信息为："+sysUser );
        log.debug ( "关联的角色id为："+Arrays.toString ( roleIds ) );
        //默认的加密工具类
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder ();
        // 加密调用的方法是 encode
        String encode = passwordEncoder.encode(sysUser.getPassword ());
        //给新用户设置密码
        sysUser.setPassword ( encode );
        sysUserService.edit(sysUser,roleIds);
        log.debug ( MessageConst.EDIT_SYSUSER_SUCCESS );
        return new Result ( true,MessageConst.EDIT_SYSUSER_SUCCESS );
    }
    //删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug ( "删除的用户id为："+id );
        sysUserService.delById(id);
        log.debug ( MessageConst.DELETE_SYSUSER_SUCCESS );
        return new Result ( true,MessageConst.DELETE_SYSUSER_SUCCESS );
    }
    //新增用户时校验手机是否已注册
    @RequestMapping("/findByTelephone")
    public Result findByTelephone(String telephone){
        log.debug ( "手机号为："+telephone );
        return sysUserService.findByTelephone(telephone);
    }
    //编辑用户时校验手机是否已注册
    @RequestMapping("/findByTelephoneAndId")
    public Result findByTelephoneAndId(String telephone,Integer id){
        log.debug ( "编辑后的手机号为："+telephone );
        log.debug ( "编辑的用户id为："+id );
        return sysUserService.findByTelephoneAndId(telephone,id);
    }
    //查询当前用户名是否已被注册
    @RequestMapping("/findBySysUserName")
    public Result findBySysUserName(String username){
        log.debug ( "用户名为："+username );
        return sysUserService.findBySysUserName(username);
    }
    //查询用户名是否重名
    @RequestMapping("/findBySysUserNameAndId")
    public Result findBySysUserNameAndId(String username,Integer id){
        log.debug ( "用户名为："+username );
        log.debug ( "用户id为："+id );
        return sysUserService.findBySysUserNameAndId(username,id);

    }
}
